#!/bin/sh /etc/rc.common

# Copyright (C) 2021 CZ.NIC z.s.p.o. (https://www.nic.cz/)

START=70

USE_PROCD=1
PROG=/usr/bin/miniflux.app

start_service() {
	local config_file
	config_load miniflux

	procd_open_instance
	procd_set_param command "$PROG"
	procd_append_param user miniflux

	config_get config_file main config_file
	if [ -n "$config_file" ]; then
		procd_append_param command -config-file="$config_file"
	else
		local listen_address run_migrations database_url
		config_get listen_address main listen_address
		procd_append_param env LISTEN_ADDR="$listen_address"
		config_get run_migrations main run_migrations
		[ "$run_migrations" = 1 ] && procd_append_param env RUN_MIGRATIONS=1
		config_get base_url main base_url
		[ -n "$base_url" ] && procd_append_param env BASE_URL="$base_url"

		config_get database_url database url
		if [ -z "$database_url" ]; then
			local database_user database_password database_dbname database_sslmode
			config_get database_user database user
			config_get database_password database password
			config_get database_dbname database dbname
			config_get database_sslmode database sslmode
			database_url="user=$database_user password=$database_password dbname=$database_dbname sslmode=$database_sslmode"
		fi
		procd_append_param env DATABASE_URL="$database_url"
	fi

	procd_set_param file "$config_file"
	procd_set_param respawn
	procd_set_param stderr 1
	procd_close_instance
}
